﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using UserUtility;
using BLL;


namespace TrainingWorkshop
{
    public partial class EditUser : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            Bind();
        }
        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    Bind();
        //}
        private void Bind()
        {

            string uid = Request["UserId"];
            if (uid == null)
            {
                Response.Write("<script>alert('Failed ')</script>");

            }

             int userid = int.Parse(uid);
             IUserRepository userRepository = UserRepositoryFactory.InitUserRepository();
             User u = userRepository.GetOneUser(userid);
             //u.UserId = userid;
             EditFirstNameTextBox.Text =u.FirstName ;
             EditLastNameTextBox.Text = u.LastName;
             EditDepartmentDropDownList.SelectedValue = u.departmentId.DepartmentId.ToString();
             EditRoleDropDownList.SelectedValue = u.roleId.RoleId.ToString();
            
            //EditFirstNameTextBox.Text = Request.QueryString["FirstName"];
            //EditLastNameTextBox.Text = Request.QueryString["LastName"];

            //string dpName = Request.QueryString["DepartmentName"];
            //EditDepartmentNameDropDownList.Text = Request.QueryString["DepartmentName"];
            //EditRoleNameDropDownList.Text = Request.QueryString["RoleName"];

            //DataTable DepartmentDDL = BLL.UserManager.GetAllDepartment();
            //EditDepartmentDropDownList.DataSource = DepartmentDDL;
            //EditDepartmentDropDownList.SelectedIndex = GetDPIndex(DepartmentDDL, dpName);


             EditDepartmentDropDownList.DataSource = userRepository.GetAllDepartment();

            
            EditDepartmentDropDownList.DataTextField = "DepartmentName";
            EditDepartmentDropDownList.DataValueField = "DepartmentId";
            EditDepartmentDropDownList.DataBind();


            EditRoleDropDownList.DataSource = userRepository.GetAllRole();

            EditRoleDropDownList.DataTextField = "RoleName";
            EditRoleDropDownList.DataValueField = "RoleId";
            EditRoleDropDownList.DataBind();


            
        }

        //private int GetDepartmentIndex(DataTable dt, string expectedDP)
        //{
        //    int index = 0;
        //    for(int i = 0; i < dt.Rows.Count; i++)
        //    {
        //        if(dt.Rows[i].ItemArray[1].ToString() == expectedDP)
        //        {
        //            index = i;
        //            break;
        //        }
        //    }
        //    return index;
        //}

        protected void EditUpdateButton_Click(object sender, EventArgs e)
        {   
            User User = new User();
            User.UserId = int.Parse(Request.QueryString["UserId"]);
            User.FirstName = EditFirstNameTextBox.Text;
            User.LastName = EditLastNameTextBox.Text;
            User.departmentId.DepartmentId = int.Parse(EditDepartmentDropDownList.SelectedValue);
            User.roleId.RoleId = int.Parse(EditRoleDropDownList.SelectedValue);
            IUserRepository userRepositoryEdit = UserRepositoryFactory.InitUserRepository();
            userRepositoryEdit.Modify(User);
            //if (editnew)
            
                Response.Write("<script>alert('Modify successfully');window.location.href='UserList.aspx'</script>");
                //Bind();
            //}
            //else
            //{
            //    Response.Write("<script>alert('Failed ')</script>");
            //}
            
        }

       
    }
}